/**
 * Copyright (c) 2011 Daniel Centore
 *
 * ex: set filetype=java expandtab tabstop=4 shiftwidth=4 :
 * 
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 */
package wiiWired.controller.data;

import java.util.HashMap;

import wiiWired.controller.console.RaceTableModel;

/**
 * Class representing a single stored race
 * 
 * @author Daniel Centore
 * 
 */
public class Race
{
	public static int raceCount = -1; //the number that the NEXT race will be

	public final int raceNumber = ++raceCount; //the race this instance is representing

	private HashMap<String, String> moteTimes = new HashMap<String, String>(); // bta, times
	private HashMap<String, String> nunchukTimes = new HashMap<String, String>(); // bta, times

	public Race()
	{
	}

	public void addMoteTime(String bta, long time)
	{
		if (!moteTimes.containsKey(bta))
		{
			moteTimes.put(bta, RaceTableModel.formatTime(time));
		}
		else
		{
			String s = moteTimes.get(bta);
			s += "\n" + RaceTableModel.formatTime(time);
			moteTimes.put(bta, s);
		}
	}
	
	public void addNunchukTime(String bta, long time)
	{
		if (!nunchukTimes.containsKey(bta))
		{
			nunchukTimes.put(bta, RaceTableModel.formatTime(time));
		}
		else
		{
			String s = nunchukTimes.get(bta);
			s += "\n" + RaceTableModel.formatTime(time);
			nunchukTimes.put(bta, s);
		}
	}

	//	public final List<RaceTime> times; //list of times within the race
	//
	//	/**
	//	 * Creates a race
	//	 * @param motes All the remotes' times
	//	 * @param nunchuks All the nunchuks' times
	//	 * @param controller The {@link MoteController}
	//	 */
	//	public Race(long[][] motes, long[][] nunchuks, MoteController[] controller)
	//	{
	//		raceNumber = raceCount;
	//
	//		times = new ArrayList<RaceTime>();
	//
	//		String[] moteString = new String[motes.length];
	//		String[] nunchukString = new String[nunchuks.length];
	//
	//		initializeString(moteString);
	//		initializeString(nunchukString);
	//
	//		for (int i = 0; i < motes.length; i++)
	//		{
	//			for (long l : motes[i])
	//				moteString[i] += RaceTableModel.formatTime(l) + "\n";
	//		}
	//
	//		for (int i = 0; i < nunchuks.length; i++)
	//		{
	//			for (long l : nunchuks[i])
	//				nunchukString[i] += RaceTableModel.formatTime(l) + "\n";
	//		}
	//
	//		for (int i = 0; i < moteString.length; i++)
	//			times.add(new RaceTime(moteString[i], nunchukString[i], controller[i]));
	//	}

	public Object getNunchukTimes(String bta)
	{
		return (nunchukTimes.containsKey(bta) ? nunchukTimes.get(bta) : "-");
	}

	public Object getTimes(String bta)
	{
		return (moteTimes.containsKey(bta) ? moteTimes.get(bta) : "-");
	}

	//	private void initializeString(String[] s) //helper
	//	{
	//		for (int i = 0; i < s.length; i++)
	//			s[i] = "";
	//	}
	//
	//	/**
	//	 * Returns the time that a single mote had
	//	 * @param bta The bluetooth address of that mote
	//	 * @return The time that a single mote had
	//	 */
	//	public String getTimes(String bta)
	//	{
	//		for (RaceTime t : times)
	//			if (t.btAddress.equals(bta))
	//				return t.time;
	//
	//		return "-"; //mote didn't exist
	//	}
	//
	//	/**
	//	 * Same as getTime(..) but for nunchuks
	//	 * @param bta
	//	 * @return
	//	 */
	//	public String getNunchukTimes(String bta)
	//	{
	//		for (RaceTime t : times)
	//			if (t.btAddress.equals(bta))
	//				return t.nunchukTime;
	//
	//		return "-";
	//	}
	//
	//	/**
	//	 * Class representing a single race time
	//	 * 
	//	 * @author Daniel Centore
	//	 * 
	//	 */
	//	private class RaceTime
	//	{
	//		private String time; //the times
	//		private String nunchukTime; //the nunchuk time
	//		private String btAddress; //bluetooth address of the mote
	//
	//		private RaceTime(String _time, String _nunchukTime, MoteController _mote)
	//		{
	//			time = _time;
	//			nunchukTime = _nunchukTime;
	//			btAddress = _mote.getBluetoothAddress();
	//		}
	//	}

}
